package com.company.woye.setting;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.company.woye.MyApplication;
import com.company.woye.R;
import com.company.woye.base.BaseActivity;
import com.company.woye.utils.DBUtil;
import com.company.woye.utils.SharedPreferenceUtils;

import org.json.JSONArray;
import org.json.JSONException;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 设置
 */
public class SettingActivity extends BaseActivity {

    @BindView(R.id.tv_top_subject)
    TextView tvTopSubject;
    @BindView(R.id.iv_head)
    ImageView ivHead;
    @BindView(R.id.tv_login_no)
    TextView tvLoginNo;
    @BindView(R.id.tv_login_name)
    TextView tvLoginName;
    @BindView(R.id.tv_factory)
    TextView tvFactory;
    @BindView(R.id.tv_departName)
    TextView tvDepartName;
    @BindView(R.id.tv_workshop)
    TextView tvWorkshop;
    @BindView(R.id.tv_group)
    TextView tvGroup;
    @BindView(R.id.ed_work_nol)
    EditText ed_work_nol;
    @BindView(R.id.ed_work_nor)
    EditText ed_work_nor;
    @BindView(R.id.ed_work_rsattion)
    EditText ed_work_rsattion;
    @BindView(R.id.ed_work_lsattion)
    EditText ed_work_lsattion;
    @BindView(R.id.ed_rock)
    EditText edRock;
    @BindView(R.id.ed_loose)
    EditText edLoose;
    @BindView(R.id.tv_ResultInfo)
    TextView tv_ResultInfo;
    MyApplication myApplication;
    private final static int MSG_Error = 9000;
    private final static int SaveSetInfo = 9001;
    JSONArray detailJSON;
    private String TAG=this.getClass().getSimpleName();

    /**
     * [{"LoginType":"A","LoginCheck":"OK","LoginUserNo":"808080","LoginUser":"测试员","LoginTime":"2021-03-06 16:16:56",
     * "UserPower":"[{\"FuncInStorageA\":1},{\"FuncInStorageB\":1},{\"FuncOutStorageA\":1},{\"FuncOutStorageB\":1},
     * {\"FuncOutStorageC\":1},{\"FuncCheck9999\":1},{\"FuncCheck9998\":1},{\"FuncCheck9997\":1},{\"FuncCheck9996\":1},
     * {\"FuncInBagSingle\":1},{\"FuncInBagWhole\":1},{\"FuncQueryStorage\":1},{\"FuncInBagQuery\":1}]","
     * JobNos":"808080,808081","Factory":"","Bbm":"制衣二部","CjName":"制衣二工场","DepartMentNo":"
     * 200101","DepartMent":"1-1","StationNos":"左B00S1001、右B00S1002","4PPlaceNo":"B00X1001",
     * "LooseClothRackNo":"MT11456"}]
     *
     * @param savedInstanceState
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }


    @Override
    public int setLayoutId() {
        return R.layout.activity_setting;
    }

    @Override
    public void initView() {
        tvTopSubject.setText("个人信息");
        myApplication=(MyApplication) getApplication();
        tvLoginNo.setText("登录账户:"+myApplication.curLoginUserNo);
        tvLoginName.setText("用户名:"+myApplication.LoginUser);
        tvFactory.setText("工厂:"+myApplication.Factory);
        tvDepartName.setText("部门:"+myApplication.Bbm);
        tvWorkshop.setText("车间:"+myApplication.CjName);
        tvGroup.setText("组别:"+myApplication.DepartMent);
        ed_work_rsattion.setText((String) SharedPreferenceUtils.getData(sharedPreferences, "rightGongWei", ""));
        ed_work_lsattion.setText((String) SharedPreferenceUtils.getData(sharedPreferences, "leftGongWei", ""));
        edLoose.setText(myApplication.LooseClothRackNo);
        edRock.setText(myApplication.PPlaceNo);
        ed_work_nol.setText(myApplication.JobNosL);
        ed_work_nor.setText(myApplication.JobNosR);
    }


    @OnClick({R.id.iv_back, R.id.tv_sure})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_sure:
                String SqlComm = "Exec AGV_Basic_SaveLoadSetInfo @UserNo='" + MyApplication.curLoginUserNo + "',@OperType='" + "SaveSetInfo" + "',@JobNoL='" + ed_work_nol.getText().toString().trim()
                        + "',@JobNoR='" + ed_work_nor.getText().toString().trim() + "',@StationNoL='" + ed_work_lsattion.getText().toString().trim() + "',@StationNoR='"+ed_work_rsattion.getText().toString().trim()+"',@BagRackNo='" +edRock.getText().toString().trim()
                        + "',@LooseRackNo='" + edLoose.getText().toString().trim()+"'";
                Log.e(TAG, SqlComm);
                //调用WebService过程并处理界面更新;
                ReadSQLData(SqlComm);
                break;
        }
    }

    //直接连获取数据，与Handler配套使用;
    private void ReadSQLData(final String SQLComm) {
        System.out.println("SQLComm:" + SQLComm);
        Runnable run = new Runnable() {
            @Override
            public void run() {
                try {
                    JSONArray jsonArray = DBUtil.QuerySQL2JSON(SQLComm);
                    System.out.println(jsonArray);
                    String OperType = "";
                    if (jsonArray.optJSONObject(0).has("OperType"))
                        OperType = jsonArray.optJSONObject(0).getString("OperType");
                    //往界面转数据;
                    if ((jsonArray != null) && (jsonArray.length() > 0)) {
                        if (OperType.equals("SaveSetInfo") == true) {
                            detailJSON = jsonArray;
                            //刷新界面;
                            Message msg = new Message();
                            msg.what = SaveSetInfo;
                            handler.removeMessages(msg.what);
                            handler.sendMessage(msg);
                        }
                    } else {
                        Message msg = new Message();
                        msg.what = MSG_Error;
                        handler.removeMessages(msg.what);
                        handler.sendMessage(msg);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    //刷新界面;
                    Message msg = new Message();
                    msg.what = MSG_Error;
                    handler.removeMessages(msg.what);
                    handler.sendMessage(msg);
                }

            }

        };
        new Thread(run).start();

    }

    //通过消息来执行处理类程序,不更新UI则不一定需要通过这个;
    final Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            // TODO Auto-generated method stub
            // 根据消息ID来判断要做的事情
            int msgId = msg.what;
            switch (msgId) {
                case SaveSetInfo:
                    if (detailJSON != null && detailJSON.length() > 0) {
                        if (detailJSON.optJSONObject(0).has("ErrInfo")) {
                            try {
                                tv_ResultInfo.setText("获取信息出错..." + "\r\n" + detailJSON.optJSONObject(0).getString("ErrInfo"));
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }

                        } else {
                            String  ResultInfo="";
                            try {
                                if (detailJSON.optJSONObject(0).has("ResultInfo"))
                                    ResultInfo = detailJSON.optJSONObject(0).getString("ResultInfo");
                                tv_ResultInfo.setText(ResultInfo);
                                MyApplication.JobNosL=ed_work_nol.getText().toString().trim();
                                MyApplication.JobNosR=ed_work_nor.getText().toString().trim();
                                MyApplication.PPlaceNo=edRock.getText().toString().trim();
                                MyApplication.LooseClothRackNo=edLoose.getText().toString().trim();
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                    break;
                case MSG_Error:
                    tv_ResultInfo.setText("链接数据库错误");
                    break;
                default:
                    break;
            }
        }
    };
}
